<?php
require_once(dirname(dirname(dirname(__FILE__))) . '/app.php');

need_manager();
need_auth('team');

$now = time();
$condition = array(
	'system' => 'Y',
	"end_time > {$now}",
	'proxy_id' => $login_user['proxy_id'],
	'audit_status' => 0,
);

/* 根据项目名称或者ID筛选项目 */
$tid = intval($_GET['tid']);
$tname = strval($_GET['tname']);
$gid = intval($_GET['gid']);
$city_id = intval($_GET['city_id']);
if(0 != $tid){
	$condition['id'] = $tid;
}
if(!empty($tname)){
	$condition[] = "title like '%".mysql_escape_string($tname)."%'";
}
if(!empty($gid)){
	$condition['group_id'] = $gid;
}
if(!empty($city_id)){
	$condition['city_id'] = $city_id;
}

/* filter start */
$team_type = strval($_GET['team_type']);
if ($team_type) { $condition['team_type'] = $team_type; }
$team_id = abs(intval($_GET['team_id']));
if ($team_id) {
	$condition['id'] = $team_id;
} else { $team_id = null; }
$team_key = strval($_GET['team_key']);
if ($team_key ) {
	$condition[] = "title LIKE '%".mysql_escape_string($team_key)."%'";
}
/* filter end */

$count = Table::Count('team', $condition);
list($pagesize, $offset, $pagestring) = pagestring($count, 20);

$teams = DB::LimitQuery('team', array(
	'condition' => $condition,
	'order' => 'ORDER BY id DESC',
	'size' => $pagesize,
	'offset' => $offset,
));
$cities = Table::Fetch('category', Utility::GetColumn($teams, 'city_id'));
$groups = Table::Fetch('category', Utility::GetColumn($teams, 'group_id'));
$partners = Table::Fetch('partner', Utility::GetColumn($teams, 'partner_id'));

//查出所有城市
$citys = DB::LimitQuery('category', array(
	'condition' => array( 'zone' => 'city', 'display' => 'Y', ),'order' => 'ORDER BY `sort_order` DESC, `id` DESC',
));

/* 初始化筛选下拉框 groupss */
$groupss = DB::LimitQuery('category', array(
			'condition' => array( 'zone' => 'group' ,'fid' => 0 ),
			));
$groupss = Utility::OptionArray($groupss, 'id', 'name');
$selector = 'index';
include template('manage_team_index');
